<template>
  <div class="admin-page">
    <div class="search-bar">
      <el-input
        style="width: 200px; margin-right: 15px"
        v-model="inp"
        placeholder="请输入管理员姓名"
      ></el-input>
      <el-button type="primary" @click="search" style="margin-right: 1350px;">查询</el-button>
      <el-button type="primary" @click="dialogVisible = true"
        >添加管理员</el-button
      >
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="编号" width="300px"></el-table-column>
      <el-table-column
        prop="username"
        label="姓名"
        width="180"
      ></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column prop="avatar" label="头像">
        <template v-slot="scope">
          <el-avatar
            size="medium"
            :src="
              scope.row.avatar
                ? scope.row.avatar
                : 'https://img0.baidu.com/it/u=739011191,1281948250&fm=253&fmt=auto&app=138&f=PNG?w=475&h=475'
            "
          ></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态">
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.state"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            disabled
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column width="180" label="操作">
        <template slot-scope="scope">
          <el-button @click="flag(scope.row.id)" size="small" type="primary"
            >编辑</el-button
          >
          <el-button @click="del(scope.row.id)" size="small" type="danger"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>

    <!-- 弹出层 -->
    <!-- 管理员添加 -->
    <el-dialog title="管理员添加" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="addInfo"
        ref="addInfo"
        label-width="100px"
        class="demo-dynamic"
      >
        <el-form-item
          prop="username"
          label="用户名称"
          :rules="[{ required: true, message: '请输入用户名称' }]"
        >
          <el-input type="text" v-model="addInfo.username"></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          label="用户密码"
          :rules="[{ required: true, message: '请输入用户名称' }]"
        >
          <el-input type="password" v-model="addInfo.password"></el-input>
        </el-form-item>
        <el-form-item
          prop="gender"
          label="用户性别"
          :rules="[{ required: true, message: '请输入用户名称' }]"
        >
          <el-input type="text" v-model="addInfo.gender"></el-input>
        </el-form-item>
        <el-form-item
          prop="avatar"
          label="用户头像"
          :rules="[{ required: true, message: '请输入用户名称' }]"
        >
          <el-upload
            class="avatar-uploader"
            action="http://8.140.198.52:8001/common/uploadFile"
            enctype="multipart/form-data"
            method="post"
            :headers="header"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img style="width: 200px; height: 200px;" v-if="addInfo.avatar" :src="addInfo.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sub">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 管理员修改 -->
    <el-dialog title="管理员修改" :visible.sync="dialogVisible1" width="30%">
      <el-form
        :model="setAdmin"
        ref="setAdmin"
        label-width="100px"
        class="demo-dynamic"
      >
        <el-form-item
          prop="username"
          label="用户名称"
          :rules="[{ required: true, message: '请输入用户名称' }]"
        >
          <el-input type="text" v-model="setAdmin.username"></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          label="用户密码"
          :rules="[{ required: true, message: '请输入用户名称' }]"
        >
          <el-input type="password" v-model="setAdmin.password"></el-input>
        </el-form-item>
        <el-form-item
          prop="gender"
          label="用户性别"
          :rules="[{ required: true, message: '请输入用户名称' }]"
        >
          <el-input type="text" v-model="setAdmin.gender"></el-input>
        </el-form-item>
        <el-form-item
          prop="avatar"
          label="用户头像"
          :rules="[{ required: true, message: '请输入用户名称' }]"
        >
          <el-upload
            class="avatar-uploader"
            action="http://8.140.198.52:8001/common/uploadFile"
            enctype="multipart/form-data"
            method="post"
            :headers="header"
            :show-file-list="false"
            :on-success="handleAvatarSuccess1"
          >
            <img v-if="setAdmin.avatar" :src="setAdmin.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="uploadAdmin">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  adminAll,
  addAdmin,
  reviseAdmin,
  delAdmin,
  findAdmin,
} from "@/api/users";
export default {
  data() {
    return {
      inp: "",
      header: {
        Authorization: sessionStorage.getItem("token"),
      },
      managerList: [],
      dialogVisible: false,
      dialogVisible1: false,
      addInfo: {
        username: "",
        password: "",
        gender: "",
        avatar: "",
      },
      setAdmin: {
        id: "",
        username: "",
        password: "",
        gender: "",
        avatar: "",
      },
      // 当前页码，初始化为1
      currentPage: 1,
      // 每页显示的条数，默认设置为10，可选项还有20
      pageSize: 10,
      // 总数据条数，初始为0，后续根据接口返回数据更新
      total: 0,
      // 用于表格展示的当前页数据
      tableData: [],
    };
  },
  created() {
    this.getManagerList();
  },
  methods: {
    // 获取所有管理员信息
    async getManagerList() {
      try {
        const res = await adminAll();
        if (res.status == 0) {
          this.managerList = res.adminList;
          this.total = this.managerList.length;
          // 根据当前页码和每页显示条数计算当前页要展示的数据
          this.getTableData();
        }
      } catch (error) {
        console.log(error);
      }
    },
    // 根据当前页码和每页显示条数获取当前页数据
    getTableData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.tableData = this.managerList.slice(start, end);
    },
    // 添加上传图片
    handleAvatarSuccess(pic) {
      // console.log(pic);
      this.addInfo.avatar = pic;
    },
    // 修改上传图片
    handleAvatarSuccess1(pic) {
      // console.log(pic);
      this.setAdmin.avatar = pic;
    },
    // 添加管理员
    async sub() {
      // console.log(this.addInfo)
      // const info = JSON.parse(JSON.stringify(this.addInfo))
      // console.log(info)
      try {
        const res = await addAdmin(this.addInfo);
        if (res.status == 0) {
          // 重新获取管理员数据
          this.getManagerList();
          this.addInfo = "";
          this.dialogVisible = false;
        }
      } catch (error) {
        console.log(error);
      }
    },
    flag(id) {
      this.dialogVisible1 = true;
      this.setAdmin.id = id;
    },
    // 修改管理员信息
    async uploadAdmin() {
      // console.log("修改管理员信息")
      // console.log(id)
      // console.log(this.setAdmin);

      try {
        const res = await reviseAdmin(this.setAdmin);
        if (res.status == 0) {
          // 重新获取管理员数据
          this.getManagerList();
          this.dialogVisible1 = false;
        }
      } catch (error) {
        console.log(error);
      }
    },
    // 删除管理员信息
    async del(id) {
      // console.log(id);
      try {
        const res = await delAdmin(id);
        if (res.status == 0) {
          this.getManagerList();
        }
      } catch (error) {
        console.log(error);
      }
    },
    // 搜索模块
    search() {
      console.log(this.inp);
      if (!this.inp) {
        this.getManagerList();
      } else {
        findAdmin(this.inp).then((res) => {
          // console.log(res)
          if (res.status == 0) {
            this.managerList = res.data;
            this.total = this.managerList.length;
            this.getTableData();
          }
        });
      }
    },
    // 每页条数改变时的处理函数
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getTableData();
    },
    // 当前页码改变时的处理函数
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTableData();
    },
  },
};
</script>

<style scoped>
/* 整体页面容器 */
.admin-page {
  padding: 20px;
  background-color: #f8f9fa;
}

/* 搜索栏样式 */
.search-bar {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  justify-content: space-between;
}
.search-bar el-input {
  margin-right: 10px;
  flex: 1;
  border-radius: 4px;
  border: 1px solid #ccc;
  padding: 8px 10px;
}
.search-bar el-button {
  margin-left: 10px;
  border-radius: 4px;
  padding: 6px 12px;
  transition: all 0.3s ease;
}
.search-bar el-button:hover {
  opacity: 0.8;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

/* 表格样式 */
.el-table {
  background-color: white;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}
.el-table th {
  background-color: #f0f0f0;
  color: #333;
  font-weight: normal;
  text-align: center;
  padding: 12px 0;
}
.el-table td {
  text-align: center;
  padding: 10px 0;
}
.el-table.el-avatar {
  margin: 0 auto;
  border-radius: 50%;
}
.el-table.el-switch {
  margin: 0 auto;
}
.el-table.el-button {
  margin: 0 5px;
  padding: 6px 12px;
  border-radius: 3px;
  transition: all 0.3s ease;
}
.el-table.el-button:hover {
  opacity: 0.8;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

/* 分页组件样式 */
.el-pagination {
  text-align: center;
  margin-top: 10px;
}
.el-pagination.is-background.el-pager li {
  background-color: #f0f0f0;
  color: #333;
  border-radius: 3px;
  margin: 0 3px;
  transition: all 0.3s ease;
}
.el-pagination.is-background.el-pager li:hover {
  background-color: #e0e0e0;
  cursor: pointer;
}
.el-pagination.is-background.el-pager li.active {
  background-color: #13ce66;
  color: white;
}
.el-pagination button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 弹出层通用样式 */
.el-dialog {
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
.el-dialog.el-form {
  padding: 20px;
}
.el-dialog.el-form-item {
  margin-bottom: 20px;
}
.el-dialog.el-form-item label {
  font-weight: bold;
  color: #333;
}
.el-dialog.el-input {
  width: 100%;
  border-radius: 4px;
  border: 1px solid #ccc;
  padding: 8px 10px;
}
.el-dialog.avatar-uploader {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px dashed #ccc;
  border-radius: 5px;
  width: 100px;
  height: 100px;
  margin: 0 auto;
  cursor: pointer;
}
.el-dialog.avatar-uploader img.avatar {
  width: 100%;
  height: 100%;
  border-radius: 5px;
  object-fit: cover;
}
.el-dialog.avatar-uploader i.el-icon-plus {
  font-size: 30px;
  color: #ccc;
}
.el-dialog.dialog-footer {
  display: flex;
  justify-content: flex-end;
  padding: 10px 20px;
}
.el-dialog.dialog-footer el-button {
  margin-left: 10px;
  border-radius: 4px;
  padding: 6px 12px;
  transition: all 0.3s ease;
}
.el-dialog.dialog-footer el-button:hover {
  opacity: 0.8;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}
</style>